package main.nk.mygraph;

import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.util.mxGraphTransferable;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource;
import com.mxgraph.util.mxRectangle;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class Main2 extends Application {

    public static void main(String[] args) {
        launch(args);
    }


    /**
     * @param name
     * @param icon
     * @param style
     * @param width
     * @param height
     * @param value
     */
    public static ImageView addTemplate(String name, Image icon, String style, int width, int height, Object value) {
        Image image = icon;

        final ImageView view = new ImageView(image);
        view.setFitWidth(width);
        view.setFitHeight(height);

        //设置图片的Label
       /* Label label = new Label();
        label.setTooltip(new Tooltip(name));
        label.setLabelFor(view);*/

        //创建图形
        mxCell cell = new mxCell(value, new mxGeometry(0, 0, width, height),
                style);
        cell.setVertex(true);

        mxRectangle bounds = (mxGeometry) cell.getGeometry().clone();

        final mxGraphTransferable t = new mxGraphTransferable(
                new Object[]{cell}, bounds);

        view.setOnMousePressed(new EventHandler<MouseEvent>() {
            public void handle(MouseEvent event) {
                System.out.println("键盘按下");
                mxEventSource eventSource = new mxEventSource(this);

                eventSource.fireEvent(new mxEventObject(mxEvent.SELECT, "entry",
                        view, "transferable", t, "previous", view));
            }
        });

        return view;


    }



    //添加鼠标按下事件

    @Override
    public void start(Stage primaryStage) {
        Group group = new Group();

        Scene scene = new Scene(group, 500, 500);

        Image image = new Image("/images/arrow.png");
        ImageView imageView = addTemplate("tuyi", image, "ROUNDED", 40, 40, "ROUNDED");
        group.getChildren().add(imageView);

       /*    ImageView view = new ImageView(image);
        view.setFitWidth(40);
        view.setFitHeight(40);

        //设置图片的Label
        Label label = new Label("d");
        label.setTooltip(new Tooltip("o"));


        Tooltip tooltip1 = new Tooltip("Creates a new file");
        Button button1 = new Button("New");
        button1.setTooltip(tooltip1);
        group.getChildren().addAll(button1);*/

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
